import logging

from aip import AipNlp


class NLP:
    APP_ID = "23198051"
    API_KEY = "0y9IHqyc5cIbNtfzSfVKKgVz"
    SECRET_KEY = "9ex1DBV6NhmapTEfvoKANPzHUtoeaOOx"
    client = AipNlp(APP_ID, API_KEY, SECRET_KEY)
    logger = logging.getLogger()

    def parse(self, string: str) -> set:
        """
        解析输出的字符串
        :param string:  输入的待解析字符串
        :return: 返还解析出的名词set集
        """
        resultsMap = self.client.lexer(string)
        resultSet: set = set()
        if "items" not in resultsMap:
            self.logger.error(resultsMap)
        # 筛选名词
        for item in resultsMap["items"]:
            if item["pos"] in {"n", "f", "s", "t", "nr", "ns", "nt", "nw", "nz"}:
                resultSet.add(item["item"])
            if item["ne"] in {"PER", "LOC", "ORG", "TIME"}:
                resultSet.add(item["item"])
        return resultSet
